home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / util / blank / BeyondTheDark.lha / BeyondTheDark / Developer / Source / Darkness / Darkness.c next >
C/C++ Source or Header  |  1995-02-12  |  5KB  |  222 lines

  1. /* Darkness Library */
  2.  
  3. #include <exec/memory.h>
  4. #include <exec/execbase.h>
  5. #include <graphics/gfxbase.h>
  6. #include <intuition/intuitionbase.h>
  7. #include <libraries/iffparse.h>
  8. #include <utility/tagitem.h>
  9.  
  10. #include <clib/macros.h>
  11.  
  12. #define __USE_SYSBASE 42
  13.  
  14. #include <proto/exec.h>
  15. #include <proto/graphics.h>
  16. #include <proto/intuition.h>
  17. #include <proto/utility.h>
  18.  
  19. #include <BTD.h>
  20.  
  21. struct IntuitionBase *IntuitionBase;
  22. struct GfxBase *GfxBase;
  23. struct Library *UtilityBase;
  24.  
  25.  
  26. /* #define DEBUG YES */
  27.  
  28. #ifdef DEBUG 
  29.  
  30. void KPrintF(char *,...);
  31.  
  32. #define DEBUG_PRINTF(a,b)  KPrintF(a,b);
  33. #define DEBUG_PRINT(a)     KPrintF(a)
  34. #else
  35. #define DEBUG_PRINTF(a,b)
  36. #define DEBUG_PRINT(a)
  37. #endif
  38.  
  39. #define DTAG(o) (BTD_Client+(o))
  40.  
  41. #define DP_Mode    DTAG(0)
  42. #define DP_Seconds DTAG(1)
  43.  
  44. #define DEF_SECONDS 60L
  45. #define MAX_SECONDS 3600L
  46.  
  47. #define DEF_MODES 0L
  48. #define MAX_MODES 1L
  49.  
  50. char *Modes[] = {"Stay Dark","Cycle once in a while",NULL};
  51.  
  52. struct BTDCycle DarkCycleParams[] =
  53.  {
  54.   DP_Mode,"Mode",BTDPT_CYCLE,DEF_MODES,Modes
  55.  };
  56.  
  57. struct BTDInteger DarkIntParams[] =
  58.  {
  59.   DP_Seconds,"Seconds",BTDPT_INTEGER,DEF_SECONDS,1L,MAX_SECONDS,TRUE,
  60.  };
  61.  
  62. struct BTDNode *DarkParams[] = 
  63.  {
  64.   &DarkCycleParams[0].BC_Node,
  65.   &DarkIntParams[0].BI_Node,
  66.   NULL
  67.  };
  68.  
  69. struct BTDInfo DarknessInfo =
  70.  {
  71.   BTDI_Revision,MAKE_ID('D','A','R','K'),
  72.   "Darkness Blanker","Total Darkness","Markus Illenseer 1994",
  73.   DarkParams
  74.  };
  75.  
  76. struct DarkStruct
  77.  {
  78.   struct BTDDrawInfo *BTDDrawInfo;
  79.   LONG Seconds;
  80.   LONG Mode;
  81.   LONG Time;
  82.   LONG RandN,RandF,RandI;
  83.  };
  84.  
  85. /* library stuff */
  86.  
  87. char MyBlankerName[] = "darkness.btd";
  88. char MyBlankerID[]   = "Darkness Blanker V" VERSION "." REVISION " for BTD";
  89.  
  90.  
  91. LONG MyBlankerLibInit(void)
  92.  
  93. {
  94.  if (GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L))
  95.   {
  96.    if (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L))
  97.     {
  98.      if (UtilityBase=OpenLibrary("utility.library",37L)) return TRUE;
  99.  
  100.      CloseLibrary (&IntuitionBase->LibNode);
  101.     }
  102.    CloseLibrary (&GfxBase->LibNode);
  103.   }
  104.  return FALSE;
  105. }
  106.  
  107. void MyBlankerLibFree(void)
  108.  
  109. {
  110.  CloseLibrary (UtilityBase);
  111.  CloseLibrary (&IntuitionBase->LibNode);
  112.  CloseLibrary (&GfxBase->LibNode);
  113. }
  114.  
  115. struct BTDInfo *QueryMyBlanker(void)
  116. {
  117.  return &DarknessInfo;
  118. }
  119.  
  120. void __regargs InitRandom(struct DarkStruct *DP,ULONG Instance)
  121.  
  122. {
  123.  ULONG Time[2];
  124.  
  125.  CurrentTime (&Time[0],&Time[1]);
  126.  DP->RandN=(LONG)Time[0];
  127.  
  128.  if (Time[1]<1024L) Time[1]|=1;
  129.  else Time[1]>>=10;
  130.  Time[1]^=Instance;
  131.  
  132.  DP->RandF=4*Time[1]+1;
  133.  DP->RandI=2*Time[1]+1;
  134. }
  135.  
  136. WORD __regargs Random(struct DarkStruct *DP,WORD Max)
  137. {
  138.  DP->RandN=DP->RandF*DP->RandN+DP->RandI;
  139.  if (DP->RandN<0L) DP->RandN=-DP->RandN;
  140.  
  141.  return (WORD)(DP->RandN%Max);
  142. }
  143.  
  144. #define FindTagData(l,t,d) GetTagData((t),(d),(l))
  145.  
  146. struct DarkStruct *InitMyBlanker(struct TagItem *TagList)
  147.  
  148. {
  149.  struct DarkStruct *DP;
  150.  struct BTDDrawInfo *BTDDrawInfo;
  151.  ULONG *Error,Dummy,Instance;
  152.  
  153.  if ((BTDDrawInfo=(struct BTDDrawInfo *)
  154.                    FindTagData(TagList,BTD_DrawInfo,NULL))==NULL) return NULL;
  155.  Error=(LONG *)FindTagData(TagList,BTD_Error,(ULONG)&Dummy);
  156.  if ((DP=AllocVec(sizeof(struct DarkStruct),MEMF_PUBLIC|MEMF_CLEAR))==NULL)
  157.   {
  158.    *Error=BTDERR_Memory;
  159.    return NULL;
  160.   }
  161.  
  162.  DP->BTDDrawInfo=BTDDrawInfo;
  163.  Instance=FindTagData(TagList,BTD_Instance,0L);
  164.  
  165.  InitRandom(DP,Instance);
  166.  
  167.  DP->Seconds=FindTagData(TagList,DP_Seconds,DEF_SECONDS);
  168.  DP->Mode=FindTagData(TagList,DP_Mode,DEF_MODES);
  169.  
  170.  if(DP->Mode==1L) 
  171.   {
  172.    BTDDrawInfo->BDI_Red[BTDDrawInfo->BDI_Pens[0]]=0;
  173.    BTDDrawInfo->BDI_Green[BTDDrawInfo->BDI_Pens[0]]=0;
  174.    BTDDrawInfo->BDI_Blue[BTDDrawInfo->BDI_Pens[0]]=0;
  175.    BTDDrawInfo->BDI_Changed[BTDDrawInfo->BDI_Pens[0]]=TRUE;
  176.    SetAPen (BTDDrawInfo->BDI_RPort,BTDDrawInfo->BDI_Pens[0]);
  177.    RectFill(BTDDrawInfo->BDI_RPort,BTDDrawInfo->BDI_Left,
  178.                                    BTDDrawInfo->BDI_Top,
  179.                                    BTDDrawInfo->BDI_Left+BTDDrawInfo->BDI_Width-1,
  180.                                    BTDDrawInfo->BDI_Top+BTDDrawInfo->BDI_Height-1);
  181.   }
  182.  
  183.  
  184.  DP->Time=Random(DP,60);
  185.  
  186.  return DP;
  187. }
  188.  
  189. void EndMyBlanker(struct DarkStruct *DP)
  190.  
  191. {
  192. DEBUG_PRINT("Dark: FreeMem\n");
  193.  FreeVec (DP);
  194. }
  195.  
  196. void AnimMyBlanker(struct DarkStruct *DP)
  197.  
  198. {
  199.  ULONG Seconds,Micros;
  200.  
  201.  CurrentTime (&Seconds,&Micros);
  202.  
  203.  if(DP->Mode==0L) WaitTOF();
  204.  else
  205.   if (Seconds>=DP->Time)
  206.   {
  207.    WaitTOF();
  208.    DP->Time=Seconds+DP->Seconds;
  209.    DP->BTDDrawInfo->BDI_Red[DP->BTDDrawInfo->BDI_Pens[0]]=Random(DP,255);
  210.    DP->BTDDrawInfo->BDI_Green[DP->BTDDrawInfo->BDI_Pens[0]]=Random(DP,255);
  211.    DP->BTDDrawInfo->BDI_Blue[DP->BTDDrawInfo->BDI_Pens[0]]=Random(DP,255);
  212.    DP->BTDDrawInfo->BDI_Changed[DP->BTDDrawInfo->BDI_Pens[0]]=TRUE;
  213.   }
  214.  
  215. }
  216.  
  217. ULONG PenCountMyBlanker(struct TagItem *TagList)
  218.  
  219. {
  220.  return ((FindTagData(TagList,DP_Mode,DEF_MODES)==1)?(ULONG)1:(ULONG)0L);
  221. }
  222.